//
// Created by Tan Ke on 2024/2/15.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>


int maxArea(int* height, int heightSize)
{
    int maxAreaLeftIndex = 0;
    int maxAreaRightIndex = heightSize - 1;
    int maxAreaResult = heightSize - 1;

    if (height[maxAreaLeftIndex] < height[maxAreaRightIndex]) {
        maxAreaResult *= height[maxAreaLeftIndex];
    } else {
        maxAreaResult *= height[maxAreaRightIndex];
    }

    for (int i = maxAreaLeftIndex; i < heightSize; i ++) {
        if (height[i] < height[maxAreaLeftIndex]) {
            continue;
        }
        for (int j = maxAreaRightIndex; j > i; j --) {
            if (height[j] < height[maxAreaRightIndex]) {
                continue;
            }
            int size = (j - i);
            if (height[j] > height[i]) {
                size *= height[i];
            } else {
                size *= height[j];
            }

            if (size > maxAreaResult) {
                maxAreaResult = size;
                maxAreaLeftIndex = i;
                maxAreaRightIndex = j;
            }
        }
    }

    return maxAreaResult;
}

int main(void)
{
    int a[9] = {1,8,6,2,5,4,8,3,7};
    printf("%d\n", maxArea(a, 9));

    int a1[2] = {1, 1};
    printf("%d\n", maxArea(a1, 2));
    return 0;
}
